package com.maoye.ossp.common.web;

import java.util.Map;

public class Validation {
	/**
	 * 是否为空字符串
	 * @param obj
	 * @return
	 */
	public static boolean isNotEmpty(Object str)
	{
		return !isEmpty(str);
	}
	
	public static boolean isEmpty(Object obj)
    {
        return null == obj || obj.toString().length() == 0;
    }
	
	/**
	 * 是否为空的Map
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean isNotEmpty(Map map)
	{
		return !isEmpty(map);
	}
	
	@SuppressWarnings("unchecked")
	public static boolean isEmpty(Map map)
	{
		return null == map || map.isEmpty();
	}
	
	/**
     * 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
     *
     * @param s 原文件名
     * @return 重新编码后的文件名
     */
    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0) k += 256;
                    sb.append("%" + Integer.toHexString(k).
                            toUpperCase());
                }
            }
        }
        return sb.toString();
    }
}
